var express = require('express');
var router = express.Router();
//orc
var Tesseract = require('tesseract.js');
//upload
var multer  = require('multer');
//upload storage
var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './public/images')
  },
  filename: function (req, file, cb) {
    cb(null, file.originalname)
  }
})
var upload = multer({ storage: storage })

/* GET users listing. */
router.post('/recognize/:lang',upload.any(), function(req, res) {
	
	if(req.files[0]==undefined || req.files[0]==null){
		res.send("图片内容是空的");
		return;
	}else if(!/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(req.files[0].originalname)){
        console.log(req.files[0]);
        res.send("图片类型必须是.gif,jpeg,jpg,png中的一种");
        return;
    }else{
    	var path="../public/images/"+req.files[0].originalname;
		console.log(path);
		var image = require('path').resolve(__dirname, path);
	    Tesseract.recognize(image, {
		    lang: req.params.lang
		})
		.progress(function(info){
			console.log(info);
		})
		.then(function(data){
			console.log('done', data);
			res.send(data.text);
			//process.exit();
		});
    }
	
});

module.exports = router;
